package gui2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.SpinnerDateModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JScrollPane;

import schedule.AppointmentList;
import schedule.Appointment;

public class Search extends JFrame {

	private JPanel contentPane;
	private JTextField tfTitle;
//	private JTextField tfStart;
	private ButtonGroup btnGroup = new ButtonGroup();
	private JRadioButton rdbtnTitlesearch;
	private JRadioButton rdbtnStartsearch;
	private JRadioButton rdbtnSearchBoth;
	private JTextArea textArea;
	private JSpinner spStart;
	private SpinnerDateModel startDate = new SpinnerDateModel(); 
	
	private AppointmentList list = new AppointmentList();

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Search frame = new Search();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Search(AppointmentList list) {
		this.list = list;
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 409);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel_4 = new JPanel();
		contentPane.add(panel_4, BorderLayout.NORTH);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_4.add(panel_1);
		
		JPanel panel = new JPanel();
		panel_1.add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(2, 2, 0, 0));
		
		JLabel lblTitle = new JLabel("Title:");
		panel.add(lblTitle);
		
		tfTitle = new JTextField();
		panel.add(tfTitle);
		tfTitle.setColumns(10);
		
		JLabel lblStart = new JLabel("Start:");
		panel.add(lblStart);
		
//		tfStart = new JTextField();
//		panel.add(tfStart);
//		tfStart.setColumns(10);
		
		spStart = new JSpinner(startDate);
		spStart.setEditor(new JSpinner.DateEditor(spStart, "dd.MM.yyyy"));
		panel.add(spStart);
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 1, 0, 0));
		
		rdbtnTitlesearch = new JRadioButton("Titlesearch");
		btnGroup.add(rdbtnTitlesearch);
		panel_2.add(rdbtnTitlesearch);
		
		rdbtnStartsearch = new JRadioButton("Startsearch");
		btnGroup.add(rdbtnStartsearch);
		panel_2.add(rdbtnStartsearch);
		
		rdbtnSearchBoth = new JRadioButton("search both");
		btnGroup.add(rdbtnSearchBoth);
		panel_2.add(rdbtnSearchBoth);
		
		btnGroup.setSelected(rdbtnTitlesearch.getModel(), true);
		
		JPanel panel_3 = new JPanel();
		panel_4.add(panel_3, BorderLayout.SOUTH);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnSearch_actionPerformed(arg0);
			}
		});
		panel_3.add(btnSearch);
		
		textArea = new JTextArea();
		textArea.setEditable(false);
//		contentPane.add(textArea, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane(textArea);
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		
		
		this.setTitle("Search");
		this.setVisible(true);
	}

	protected void do_btnSearch_actionPerformed(ActionEvent arg0){
		List<Appointment> searchList = new AppointmentList();
		textArea.setText("");
		if(btnGroup.isSelected(rdbtnTitlesearch.getModel())){
			 try {
				searchList = list.searchTitle(tfTitle.getText());
			} catch (Exception e) {
//				e.printStackTrace();
				JOptionPane.showMessageDialog(this, e.getMessage());
			}
			 for(int i=0;i<searchList.size();i++)
				 textArea.append(searchList.get(i).getTitle()+"\nStart: "+searchList.get(i).getDate()+"\nEnd: "+searchList.get(i).getEnd()+"\n"+searchList.get(i).getDescription()+"\n\n");
		}
		if(btnGroup.isSelected(rdbtnStartsearch.getModel())){
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//			Date convertedDate;
			try {
//				convertedDate = (Date) sdf.parse(tfStart.getText());
//				searchList = list.searchBeginningDay(convertedDate);
				searchList = list.searchBeginningDay(startDate.getDate());
			} catch (ParseException pe) {
//				e.printStackTrace();
				JOptionPane.showMessageDialog(this, pe.getMessage());
			} catch (Exception e) {
//				e.printStackTrace();
				JOptionPane.showMessageDialog(this, e.getMessage());
			}
			for(int i=0;i<searchList.size();i++)
				 textArea.append(searchList.get(i).getTitle()+"\nStart: "+searchList.get(i).getDate()+"\nEnd: "+searchList.get(i).getEnd()+"\n"+searchList.get(i).getDescription()+"\n\n");
		}
		if(btnGroup.isSelected(rdbtnSearchBoth.getModel())){
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//			Date convertedDate;
			try {
//				convertedDate = (Date) sdf.parse(tfStart.getText());
//				searchList = list.searchTitleAndDate(tfTitle.getText(), convertedDate);
				searchList = list.searchTitleAndDate(tfTitle.getText(), startDate.getDate());
			} catch (ParseException pe) {
//				e.printStackTrace();
				JOptionPane.showMessageDialog(this, pe.getMessage());
			} catch (Exception e) {
//				e.printStackTrace();
				JOptionPane.showMessageDialog(this, e.getMessage());
			}
			for(int i=0;i<searchList.size();i++)
				 textArea.append(searchList.get(i).getTitle()+"\nStart: "+searchList.get(i).getDate()+"\nEnd: "+searchList.get(i).getEnd()+"\n"+searchList.get(i).getDescription()+"\n\n");
		}
	}
}
